﻿using System.ComponentModel;
using System.Windows.Threading;
using Microsoft.Practices.Composite.Events;
using Microsoft.Practices.Composite.Wpf.Events;
using WpfTwitterClient.Infrastructure.Events;
using WpfTwitterClient.Infrastructure.Models;
using WpfTwitterClient.Infrastructure.Services;

namespace WpfTwitterClient.Modules.Timelines.Views.HomeTimeline
{
    public class HomeTimelinePresenter : IHomeTimelinePresenter
    {
        public IEventAggregator eventAggregator;
        private delegate Tweets MyDelegate();
        private ITwitterClient Service { get; set; }

        public HomeTimelinePresenter(IHomeTimelineView view, ITwitterClient service, IEventAggregator eventAggregator)
        {
            this.View = view;
            this.View.Model = new HomeTimelineModel(((DispatcherObject)view).Dispatcher);
            this.Service = service;
            this.eventAggregator = eventAggregator;

            eventAggregator.GetEvent<ConnectedEvent>().Subscribe(ConnectedEventHandler, ThreadOption.UIThread, true);
            eventAggregator.GetEvent<LoadedTweetsEvent>().Subscribe(LoadedTweetsEventHandler, ThreadOption.UIThread, true);
            eventAggregator.GetEvent<ApplicationResetEvent>().Subscribe(ApplicationResetEventHandler, ThreadOption.UIThread, true);
            
            //var tweets = Service.GetHomeTimeline();
            //this.View.Model.SetTweets(tweets);
        }

        private void ApplicationResetEventHandler(string arg)
        {
            this.View.Model.SetTweets(null);
        }

        public void LoadedTweetsEventHandler(Tweets tweets)
        {
            this.View.Model.SetTweets(tweets);
        }

        public void ConnectedEventHandler(string arg)
        {
            eventAggregator.GetEvent<LoadingTweetsEvent>().Publish("");
            GetTweetsAsync();
        }

        public void GetTweetsAsync()
        {
            eventAggregator.GetEvent<StatusEvent>().Publish("Getting Tweets..");
            
            var worker = new BackgroundWorker();
            worker.WorkerReportsProgress = true;
            worker.DoWork += new DoWorkEventHandler(worker_DoWork);
            worker.ProgressChanged += new ProgressChangedEventHandler(worker_ProgressChanged);
            worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(worker_RunWorkerCompleted);
            worker.RunWorkerAsync();
        }

        void worker_DoWork(object sender, DoWorkEventArgs e)
        {
            var bw = sender as BackgroundWorker;
            e.Result = Service.BeginGetHomeTimeline(bw);
        }

        void worker_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            eventAggregator.GetEvent<StatusEvent>().Publish(string.Format("Loading {0}%", e.ProgressPercentage));            
        }

        void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            if (e.Error == null)
            {
                var tweets = e.Result as Tweets;
                eventAggregator.GetEvent<LoadedTweetsEvent>().Publish(tweets);
                eventAggregator.GetEvent<StatusEvent>().Publish(string.Format("Loaded {0} tweets", tweets.Count));
            }
            else
            {
                eventAggregator.GetEvent<StatusEvent>().Publish("Failed to load Timeline");
            }
        }

        public IHomeTimelineView View { get; set; }

    }
}
